﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;
using QuickModel.Models;
using QuickModel.QuickModelFrame;

namespace QuickModel.LastFastModeling.AddLabel
{
	[Rebuilder(RebuilderName = "AddLabel")]
	public class AddLabelRebuilder : IRevitModelRebuilder
	{
		public bool TryRebuildRevitModel(Autodesk.Revit.DB.Document inputDoc, RevitModelRequest inputReqeust, out BuildData buildData)
		{
			buildData = new BuildData();
			buildData.NeedResponse = false;
			bool result;
			try
			{
				AddLabelRevitModelRequest addLabelRevitModelRequest = inputReqeust as AddLabelRevitModelRequest;
				bool flag = addLabelRevitModelRequest == null || addLabelRevitModelRequest.CreateInstanceRequests == null || addLabelRevitModelRequest.CreateInstanceRequests.Count <= 0;
				if (flag)
				{
					result = false;
				}
				else
				{
					QuickModelingPara quickModelingPara = addLabelRevitModelRequest.ViewParas as QuickModelingPara;
					bool flag2 = quickModelingPara == null;
					if (flag2)
					{
						result = false;
					}
					else
					{
						string labelText = string.Empty;
						HydrantViewPara hydrantViewPara = quickModelingPara.ChildViewPara as HydrantViewPara;
						bool flag3 = hydrantViewPara == null || string.IsNullOrEmpty(hydrantViewPara.LabelText) || hydrantViewPara.LabelText.Trim() == "";
						if (flag3)
						{
							result = false;
						}
						else
						{
							labelText = hydrantViewPara.LabelText.Trim();
							string text = string.Empty;
							Transaction transaction = new Transaction(inputDoc, "Replace");
							try
							{
								transaction.Start();
								bool flag4 = addLabelRevitModelRequest.CreateInstanceRequests.Count == 1;
								if (flag4)
								{
									bool flag5 = this.CreateOneLabel(inputDoc, addLabelRevitModelRequest.CreateInstanceRequests[0], labelText);
								}
								else
								{
									bool flag6 = addLabelRevitModelRequest.CreateInstanceRequests.Count == 2;
									if (flag6)
									{
										bool flag5 = this.CreateTwoLabel(inputDoc, addLabelRevitModelRequest.CreateInstanceRequests, labelText);
									}
								}
							}
							catch (Exception ex)
							{
								text = ex.Message;
								transaction.RollBack();
							}
							finally
							{
								bool flag7 = (int)transaction.GetStatus() == 1;
								if (flag7)
								{
									transaction.Commit();
								}
							}
							result = true;
						}
					}
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool CreateOneLabel(Autodesk.Revit.DB.Document doc, LastFastModelingRevitModelRequest createInstanceRequest, string labelText)
		{
			FamilyInstance familyInstance = createInstanceRequest.CreatedElement as FamilyInstance;
			bool flag = familyInstance == null || familyInstance.Symbol == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				XYZ blockActualLocation = BussinessCommon.GetBlockActualLocation(createInstanceRequest.Block);
				XYZ blockFaceDirection = BussinessCommon.GetBlockFaceDirection(createInstanceRequest.Block);
				bool flag2 = blockActualLocation == null || blockFaceDirection == null;
				if (flag2)
				{
					result = false;
				}
				else
				{
					XYZ xyz = (-blockFaceDirection).CrossProduct(new XYZ(0.0, 0.0, -1.0));
					FamilySymbol symbol = familyInstance.Symbol;
					double num = 0.0;
					bool flag3 = !this.GetInstanceLength(symbol, out num);
					if (flag3)
					{
						result = false;
					}
					else
					{
						double num2 = 0.0;
						double num3 = 0.0;
						ConnectHelper.GetTagTextWidthAndHeight(labelText, doc, ref num2, ref num3);
						double moveDistance = num3 / 304.8 + num;
						double markMoveDistance = this.GetMarkMoveDistance(moveDistance, doc, 1);
						XYZ origin = blockActualLocation + markMoveDistance * blockFaceDirection;
						TextNote textNode = doc.GetTextNode(doc.ActiveView, origin, xyz, xyz.CrossProduct(new XYZ(0.0, 0.0, 1.0)), (double)StringTools.GetStringByteLength(labelText) / 100.0, (Autodesk.Revit.DB.TextAlignFlags)128, labelText);
						bool flag4 = textNode != null;
						if (flag4)
						{
							textNode.TextNoteType.GetParameter(BuiltInParameter.TEXT_BACKGROUND).Set(1);
						}
						result = false;
					}
				}
			}
			return result;
		}

		private bool CreateTwoLabel(Autodesk.Revit.DB.Document doc, List<LastFastModelingRevitModelRequest> createInstanceRequests, string labelText)
		{
			bool flag = createInstanceRequests == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				LastFastModelingRevitModelRequest lastFastModelingRevitModelRequest = createInstanceRequests[0];
				LastFastModelingRevitModelRequest lastFastModelingRevitModelRequest2 = createInstanceRequests[1];
				FamilyInstance familyInstance = lastFastModelingRevitModelRequest.CreatedElement as FamilyInstance;
				bool flag2 = familyInstance == null || familyInstance.Symbol == null;
				if (flag2)
				{
					result = false;
				}
				else
				{
					XYZ xyz = BussinessCommon.GetBlockActualLocation(lastFastModelingRevitModelRequest.Block);
					XYZ blockFaceDirection = BussinessCommon.GetBlockFaceDirection(lastFastModelingRevitModelRequest.Block);
					XYZ blockActualLocation = BussinessCommon.GetBlockActualLocation(lastFastModelingRevitModelRequest2.Block);
					bool flag3 = xyz == null || blockFaceDirection == null || blockActualLocation == null;
					if (flag3)
					{
						result = false;
					}
					else
					{
						xyz = (xyz + blockActualLocation) / 2.0;
						XYZ xyz2 = (-blockFaceDirection).CrossProduct(new XYZ(0.0, 0.0, -1.0));
						FamilySymbol symbol = familyInstance.Symbol;
						double num = 0.0;
						bool flag4 = !this.GetInstanceLength(symbol, out num);
						if (flag4)
						{
							result = false;
						}
						else
						{
							double num2 = 0.0;
							double num3 = 0.0;
							ConnectHelper.GetTagTextWidthAndHeight(labelText, doc, ref num2, ref num3);
							double moveDistance = num3 / 304.8 + num;
							double markMoveDistance = this.GetMarkMoveDistance(moveDistance, doc, 1);
							XYZ origin = xyz + markMoveDistance * blockFaceDirection;
							TextNote textNode = doc.GetTextNode(doc.ActiveView, origin, xyz2, xyz2.CrossProduct(new XYZ(0.0, 0.0, 1.0)), (double)StringTools.GetStringByteLength(labelText) / 100.0, (Autodesk.Revit.DB.TextAlignFlags)128, labelText);
							bool flag5 = textNode != null;
							if (flag5)
							{
								textNode.TextNoteType.GetParameter(BuiltInParameter.TEXT_BACKGROUND).Set(1);
							}
							result = false;
						}
					}
				}
			}
			return result;
		}

		private double GetMarkMoveDistance(double moveDistance, Autodesk.Revit.DB.Document doc, int markStyle)
		{
			bool flag = markStyle == 0;
			if (flag)
			{
				string version = RevitVersion.Version;
				if (!(version == "2017"))
				{
					if (version == "2016")
					{
						moveDistance -= 0.41010498687664043 * (double)doc.ActiveView.Scale / 100.0;
					}
				}
				else
				{
					moveDistance -= 0.5741469816272966 * (double)doc.ActiveView.Scale / 100.0;
				}
			}
			bool flag2 = markStyle == 1;
			if (flag2)
			{
				string version2 = RevitVersion.Version;
				if (!(version2 == "2017"))
				{
					if (version2 == "2016")
					{
						moveDistance -= 0.32808398950131235 * (double)doc.ActiveView.Scale / 100.0;
					}
				}
				else
				{
					moveDistance -= 0.50853018372703407 * (double)doc.ActiveView.Scale / 100.0;
				}
			}
			return moveDistance;
		}

		private bool GetInstanceLength(FamilySymbol fs, out double length)
		{
			length = 0.0;
			bool flag = fs.GetFamilyNameBySymbol().Contains("推车");
			if (flag)
			{
				length = 0.95144356955380571;
			}
			else
			{
				length = 0.9120734908136483;
			}
			return true;
		}
	}
}
